import Vue from "vue"
import Vuex from "vuex"
import Cookies from 'vue-cookies'

import requestAxios from '@/assets/js/axios.js'

Vue.use(Vuex);

export default new Vuex.Store({
    state: {
        // sid: localStorage.getItem('sid') ? localStorage.getItem('sid') : '',
        sid: Cookies.get('sid') ? Cookies.get('sid') : '',
        user: Cookies.get('user') ? Cookies.get('user') : '',
        topTab: "wallet",
        logo:require('@/assets/img/logo.png'),
        logo1:require('@/assets/img/logo1.png'),
        storeMyInvite:[],
        storeReturnRecord:[],
        balanceFund:0,
        balanceManage:0,
    },
    actions: {
        refreshBalance (context) {
            requestAxios.requestWeb('post', webUserBalanceApi + '/my_balance.do', null, (response) => {
                console.log('my_balance == ', response);
                let data = {
                    balanceFund:0,
                    balanceManage:0,
                };
                for (let i = 0; i < response.data.length; i++) {
                    if (response.data[i].type == 1) {
                        data.balanceFund += response.data[i].total;
                    } else if (response.data[i].type == 2) {
                        data.balanceManage += response.data[i].total;
                    }
                }
                if(!data.balanceFund){
                    data.balanceFund = 0;
                }
                if(!data.balanceManage){
                    data.balanceManage = 0;
                }
                context.commit('changeBalance',data);
            })

        }
    },
    mutations: {
        // 修改token，并将token存入cookie
        changeLogin(state, user) {
            state.sid = user.sid;
            state.user = user.user;
            Cookies.set("sid", user.sid, '7d');
            Cookies.set("user", user.user, '7d');
        },
        changeCMSLogin(state, user) {
            state.sid = user.sid;
            state.user = user.user;
            localStorage.setItem("sid",user.sid);
            localStorage.setItem('user', JSON.stringify(user.user));
        },
        // 我的邀请人数
        myInviteFunc(state, invite){
            state.storeMyInvite = invite;
        },

        // 钱包和理财资金
        changeBalance(state, balance){
            state.balanceFund = balance.balanceFund;
            state.balanceManage = balance.balanceManage;
        },
        // 返佣记录
        returnRecordFunc(state, returnRecord){
            state.storeReturnRecord = returnRecord;
        },
        changeTab(state, tab) {
            state.topTab = tab;
        },

    }
})
